<template>
  <div class="share-base-configuration">
    <je-form-item :label="$t('formStart.name')" v-bind="validateInfos.name" name="name">
      <je-input v-model:value.trim="formState.name" @change="onNameChange" />
    </je-form-item>
    <je-form-item :label="$t('formStart.resourceId')" name="nodeCode">
      <je-select v-model:value="nodeCode" disabled config-info="JE_WF_TASKTYPE,nodeCode,code,S" />
    </je-form-item>
    <!-- <je-form-item :label="$t('formStart.formSchemeName')" name="formSchemeName">
      <je-select v-model:value="formState.formSchemeName" disabled :options="[]" />
    </je-form-item> -->
    <je-form-item
      :label="$t('FormTaskJudge_BaseConfiguration.listSynchronization')"
      name="listSynchronization"
    >
      <je-switch v-model:value="formState.listSynchronization" mode="checkbox" />
      <span class="prompt">
        {{ $t('FormTaskJudge_BaseConfiguration.listSynchronizationContext') }}
      </span>
    </je-form-item>
    <je-form-item :label="$t('FormTaskJudge_BaseConfiguration.taskRetrieveConfig')" name="retrieve">
      <je-switch v-model:value="formState.retrieve" mode="checkbox" />
      <span class="prompt">
        {{ $t('FormTaskJudge_BaseConfiguration.taskRetrieveConfigContext') }}
      </span>
    </je-form-item>
    <je-form-item :label="$t('FormTaskJudge_BaseConfiguration.taskUrgeConfig')" name="urge">
      <je-switch v-model:value="formState.urge" mode="checkbox" />
      <span class="prompt">
        {{ $t('FormTaskJudge_BaseConfiguration.taskUrgeConfigContext') }}
      </span>
    </je-form-item>
    <je-form-item
      v-if="['countersign', 'batchtask'].indexOf(nodeCode) == -1"
      :label="$t('FormTaskJudge_BaseConfiguration.taskInvalidConfig')"
      name="invalid"
    >
      <je-switch v-model:value="formState.invalid" mode="checkbox" />
      <span class="prompt">
        {{ $t('FormTaskJudge_BaseConfiguration.taskInvalidConfigContext') }}
      </span>
    </je-form-item>
    <je-form-item
      v-if="['countersign', 'batchtask', 'to_assignee'].indexOf(nodeCode) == -1"
      :label="$t('FormTaskJudge_BaseConfiguration.taskTransferConfig')"
      name="transfer"
    >
      <je-switch v-model:value="formState.transfer" mode="checkbox" />
      <span class="prompt">
        {{ $t('FormTaskJudge_BaseConfiguration.taskTransferConfigContext') }}
      </span>
    </je-form-item>
    <je-form-item
      v-if="['countersign', 'batchtask', 'to_assignee'].indexOf(nodeCode) == -1"
      :label="$t('FormTaskJudge_BaseConfiguration.taskDelegateConfig')"
      name="delegate"
    >
      <je-switch v-model:value="formState.delegate" mode="checkbox" />
      <span class="prompt">
        {{ $t('FormTaskJudge_BaseConfiguration.taskDelegateConfigContext') }}
      </span>
    </je-form-item>
    <je-form-item
      :label="$t('FormTaskJudge_BaseConfiguration.taskFormBasicConfig')"
      name="formEditable"
    >
      <je-switch v-model:value="formState.formEditable" mode="checkbox" />
      <span class="prompt">
        {{ $t('FormTaskJudge_BaseConfiguration.taskFormBasicConfigContext') }}
      </span>
    </je-form-item>
    <je-form-item :label="$t('FormTaskJudge_BaseConfiguration.taskRemindConfig')" name="remind">
      <je-switch v-model:value="formState.remind" mode="checkbox" />
      <span class="prompt">
        {{ $t('FormTaskJudge_BaseConfiguration.taskRemindConfigCentext') }}
      </span>
    </je-form-item>
    <!-- <je-form-item
      :label="$t('FormTaskJudge_BaseConfiguration.simpleApproval')"
      name="simpleApproval"
    >
      <je-switch v-model:value="formState.simpleApproval" mode="checkbox" />
      <span class="prompt">
        {{ $t('FormTaskJudge_BaseConfiguration.simpleApprovalContext') }}
      </span>
    </je-form-item> -->
    <je-form-item
      v-if="['batchtask', 'joint'].indexOf(nodeCode) != -1"
      :label="$t('FormTaskJudge_BaseConfiguration.allUserDefaultSelected')"
      name="selectAll"
    >
      <je-switch v-model:value="formState.selectAll" mode="checkbox" />
      <span class="prompt">
        {{ $t('FormTaskJudge_BaseConfiguration.allUserDefaultSelectedContext') }}
      </span>
    </je-form-item>
    <!-- <je-form-item
      v-if="['countersign'].indexOf(nodeCode) == -1"
      :label="$t('FormTaskJudge_BaseConfiguration.asyncTree')"
      name="asynTree"
    >
      <je-switch v-model:value="formState.asynTree" mode="checkbox" />
      <span class="prompt">
        {{ $t('FormTaskJudge_BaseConfiguration.asyncTreeContext') }}
      </span>
    </je-form-item> -->
    <je-form-item
      v-if="['task', 'decision', 'to_assignee'].indexOf(nodeCode) != -1"
      :label="$t('FormTaskJudge_BaseConfiguration.taskJumpConfig')"
      name="isJump"
    >
      <je-switch v-model:value="formState.isJump" mode="checkbox" />
      <span class="prompt">
        {{ $t('FormTaskJudge_BaseConfiguration.taskJumpConfigContext') }}
      </span>
    </je-form-item>
    <je-form-item
      v-if="['countersign', 'decision', 'batchtask'].indexOf(nodeCode) == -1"
      :label="$t('FormTaskJudge_BaseConfiguration.enableLogic')"
      name="logicalJudgment"
    >
      <je-switch v-model:value="formState.logicalJudgment" mode="checkbox" />
      <span class="prompt">
        {{ $t('FormTaskJudge_BaseConfiguration.enableLogicContext') }}
      </span>
    </je-form-item>
    <je-form-item
      v-if="nodeCode == 'batchtask'"
      :label="$t('FormTaskJudge_BaseConfiguration.orderApproval')"
      name="sequential"
    >
      <je-switch v-model:value="formState.sequential" mode="checkbox" />
      <span class="prompt">
        {{ $t('FormTaskJudge_BaseConfiguration.orderApprovalContext') }}
      </span>
    </je-form-item>
    <SecurityWorkflow :model="formState" />
  </div>
</template>

<script>
  import { defineComponent, ref, reactive, watch } from 'vue';
  import { Form, Input, Select, Switch } from '@jecloud/ui';
  import { verifyData } from '@/helper/common';
  import SecurityWorkflow from './security-workflow';

  const useForm = Form.useForm;
  export default defineComponent({
    name: 'ShareBaseConfiguration',
    components: {
      JeFormItem: Form.Item,
      JeInput: Input,
      JeSelect: Select,
      JeSwitch: Switch,
      SecurityWorkflow,
    },
    props: {
      nodeType: {
        type: String,
        default: '',
      },
      properties: {
        type: Object,
        default: () => {
          return {};
        },
      },
      nodeId: {
        type: String,
        default: '',
      },
      wfEditor: {
        type: Object,
        default: () => {
          return {};
        },
      },
    },
    setup(props) {
      //表单数据
      const formState = ref(props.properties);
      const nodeCode = ref(props.nodeType);

      //字段校验规则
      const rulesRef = reactive({
        name: props.nodeType == 'task' ? verifyData.name : [],
      });
      const { validate, validateInfos } = useForm(formState, rulesRef, {
        onValidate: (...args) => {},
      });

      watch(
        () => props.nodeId,
        () => {
          nodeCode.value = props.nodeType;
          formState.value = props.properties;
          if (props.nodeType == 'task') {
            rulesRef.name = verifyData.name;
          } else {
            rulesRef.name = [];
          }
        },
      );

      //名称字段change事件
      const onNameChange = () => {
        props.wfEditor.updateNodeLabel({ label: formState.value.name });
      };

      return { formState, validateInfos, validate, nodeCode, onNameChange };
    },
  });
</script>
